package com.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * rabbitmq配置
 * @author gxd
 * @since 2025/6/8
 */
@Configuration
public class RabbitMqConfig {

    @Value("${mq.exchange-name}")
    private String exchangeName;

    @Value("${mq.user-queue-name}")
    private String userQueueName;

    @Value("${mq.dept-queue-name}")
    private String deptQueueName;

    @Value("${mq.user-route-key}")
    private String userRouteKey;

    @Value("${mq.dept-route-key}")
    private String deptRouteKey;


    // 交换机
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(exchangeName);
    }

    // 用户队列
    @Bean
    public Queue userQueue() {
        return new Queue(userQueueName);
    }

    // 部门队列
    @Bean
    public Queue deptQueue() {
        return new Queue(deptQueueName);
    }

    // 绑定用户队列
    @Bean
    public Binding userBinding(DirectExchange exchange, Queue userQueue) {
        return BindingBuilder.bind(userQueue).to(exchange).with(userRouteKey);
    }

    // 绑定部门队列
    @Bean
    public Binding deptBinding(DirectExchange exchange, Queue deptQueue) {
        return BindingBuilder.bind(deptQueue).to(exchange).with(deptRouteKey);
    }
}
